class UsersController < ApplicationController
  
  protect_from_forgery :except => [:post_data]
    
  def post_data
    if params[:oper] == "del"
      User.find(params[:id]).destroy
    else
      user_params = { :id => params[:id], :name => params[:name] }
      if params[:id] == "_empty"
        user = User.create(user_params)
      else
        user = User.find(params[:id])
        user.update_attributes(user_params)
      end
    end
    
    render :nothing => true
  end

  def index
    users = User.find(:all) do
      if params[:_search] == "true"
                id =~ "%#{params[:id]}%" if params[:id].present?
                name =~ "%#{params[:name]}%" if params[:name].present?
              end
      paginate :page => params[:page], :per_page => params[:rows]      
      order_by "#{params[:sidx]} #{params[:sord]}"
    end
    if request.xhr?
      render :json => users.to_jqgrid_json([:id,:name], params[:page], params[:rows], users.total_entries) and return
    end
  end

end
